package com.Artificialintelligence.web.controller.system;

import com.Artificialintelligence.common.constant.Constants;
import com.Artificialintelligence.common.core.controller.BaseController;
import com.Artificialintelligence.common.core.domain.AjaxResult;
import com.Artificialintelligence.common.core.domain.model.RegisterBody;
import com.Artificialintelligence.common.utils.StringUtils;
import com.Artificialintelligence.system.service.ISysConfigService;
import com.Artificialintelligence.system.service.ISysUserService;
import com.Artificialintelligence.system.service.SysRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 注册验证
 *
 * @author ruoyi
 */
@RestController
public class SysRegisterController extends BaseController
{
    String uuid="emilcode";

    String verifyKey = Constants.CAPTCHA_CODE_KEY+uuid;

    @Autowired
    private SysRegisterService registerService;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @PostMapping("/register")
    public AjaxResult register(@RequestBody RegisterBody user)
    {
        int tepe=user.getTepy();
        if(tepe==1) {
            if (!("true".equals(configService.selectConfigByKey("sys.account.registerUser")))) {
                return error("当前系统没有开启注册功能！");
            }
            Map<String,Object> map = registerService.register(user);
            String msg = (String) map.get("msg");
            return StringUtils.isEmpty(msg) ? AjaxResult.success(map) : error(msg);
        }
        if(tepe==2){
            Map<String, Object> ajax = new HashMap<>();
            String from = "chenjialin200010@outlook.com";
            //生成6位随机数
            int intcode = (int)((Math.random() * 9 + 1) * 100000);
            String code = Integer.toString(intcode);
            //存入redis，过期时间3分钟
            //向redis里存入数据和设置缓存时间
            ValueOperations forValue = redisTemplate.opsForValue();
            forValue.set(user.getEmail(), code);
//            redisTemplate.opsForValue().set(user.getEmail(), code, 60 * 3, TimeUnit.SECONDS);
            redisTemplate.expire(user.getEmail(),60*3,TimeUnit.SECONDS);
            boolean lean = userService.sendSimpleMail(from,user.getEmail(),code);
            if(lean==false){
                ajax.put("Return", 5000);
                ajax.put("Tips", "发送邮件失败");
            }else {
                ajax.put("Return", 2000);
                ajax.put("Tips", "发送成功，请查看您的邮箱，有效时间为3分钟");
            }
            return AjaxResult.success(ajax);
        }
        return error();
    }

//    /**
//     * 获取验证码
//     */
//    @GetMapping("/validateCode")
//    public AjaxResult validateCode(@PathVariable(value = "email", required = false) String email)
//    {
//        String from = "chenjialin200010@outlook.com";
//        //生成6位随机数
//        int intcode = (int)((Math.random() * 9 + 1) * 100000);
//        String code = Integer.toString(intcode);
//        //存入redis，过期时间5分钟
//        redisTemplate.opsForValue().set(verifyKey + email, intcode, 5, TimeUnit.MINUTES);
//        boolean lean = userService.sendSimpleMail(from,email,code);
//        if(lean==false){
//            return error();
//        }
//        return success();
//    }
}
